package com.example.test.model;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.Text;

import com.example.test.activity.HomeActivity;
import com.example.test.activity.TopicActivity;
import com.example.test.activity.WebViewActivity;
import com.example.test.activity.WeiBoDetailActivity;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

public class StatusTextProcess {
    public static void setText(Context context , TextView view,String text,String id,String resource)
    {

		//创建一个 SpannableString对象
	SpannableString sp = new SpannableString(text);
    	if(!text.trim().equals(""))
    	{
    	Vector<Length> vector1 = setAtSpan(context, text, sp,resource);
    	Vector<Length> vector2 = setHttpSpan(context, text, sp);
    	Vector<Length> vector3 = setTopicSpan(context, text, sp,resource);
    	vector1.addAll(vector2);
    	vector1.addAll(vector3);
    	Collections.sort(vector1, new Comparator<Length>() {
    		public int compare(Length a , Length b)
    		{
    			return a.sIndex-b.sIndex;
    		}
		});
    	Vector<Length> vOther = findOtherElement(vector1, text.length());
    	setOtherSpan(context, text, sp, vOther,id,resource);
    	}
    	view.setText(sp);
    	view.setMovementMethod(LinkMovementMethod.getInstance());
    }
    //设置@我的
    private static Vector<Length> setAtSpan(Context context, String text,	SpannableString sp,String resource)
    {
    	String regx;
    	regx="@[\\S&&[^:]&&[^：]]+";
    	Vector<Length> vector1=findMatchStringArr(regx, text);
    	for (Length length : vector1) {
    		sp.setSpan(new StatusAtSpan(context,text.substring(length.sIndex,length.eIndex),resource), length.sIndex, length.eIndex, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
    	
    	
    	return vector1;
    }
  //设置##话题
    private static Vector<Length> setTopicSpan(Context context, String text,SpannableString sp,String resource)
    {
    	String regx="#[^#]+#";
    	Vector<Length> vector=findMatchStringArr(regx, text);
    	for (Length length : vector) {
    		sp.setSpan(new StatusTopicSpan(context,text.substring(length.sIndex,length.eIndex),resource), length.sIndex, length.eIndex, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
    	return vector;
    }
    //设置http链接
  //设置@我的
    private static Vector<Length> setHttpSpan(Context context, String text,	SpannableString sp)
    {
    	String regx="http:[\\S]+";
    	Vector<Length> vector=findMatchStringArr(regx, text);
    	for (Length length : vector) {
    		sp.setSpan(new StatusHttpSpan(context,text.substring(length.sIndex,length.eIndex)), length.sIndex, length.eIndex, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
    	return vector;
    }
    //设置其他部分
    private static void setOtherSpan(Context context, String text,	SpannableString sp,Vector<Length> vector,String id,String resource)
    {
    	for (Length length : vector) {
    		sp.setSpan(new StatusOtherSpan(context,text.substring(length.sIndex,length.eIndex),id,resource), length.sIndex, length.eIndex, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
    }
    //寻找符合正则匹配的字符串数组
    private static	Vector<Length> findMatchStringArr(String regx,String text)
    {
    	Vector<Length> vector =new Vector<Length>();
    	Pattern pattern=Pattern.compile(regx);
    	Matcher matcher=pattern.matcher(text);
    	while(matcher.find())
    	{
    		vector.add(new Length(matcher.start(), matcher.end()));
    	}
    	return vector;
    }
    //寻找source中其他部分
    private static  Vector<Length> findOtherElement(Vector<Length> data,int sourceLength)
	{
		Vector<Length> vResult =new Vector<Length>(); 
		if(data.size()==0)
		{
			vResult.add(new Length(0, sourceLength-1));
			return vResult;
		}
		if(data.get(0).sIndex>0)
			vResult.add(new Length(0, data.get(0).sIndex-1));
		for(int i=1;i<data.size();i++)
		{
			if(data.get(i).sIndex-data.get(i-1).eIndex>1)
				vResult.add(new Length(data.get(i-1).eIndex+1,data.get(i).sIndex-1));
		}
		if(data.get(data.size()-1).eIndex<(sourceLength-1))
			vResult.add(new Length(data.get(data.size()-1).eIndex, sourceLength-1));
		return vResult;
	}
}

class StatusAtSpan extends ClickableSpan
{
	Context context;
    String content;
    String resource;
	public StatusAtSpan(Context context,String content,String resource) {
		super();
		this.content=content;
		this.context = context;
		this.resource=resource;
	}

	@Override
	public void onClick(View widget) {
		if(resource.equals("Renn"))
		{
			return;
		}
		Intent intent=new Intent();
		intent.setClass(context, HomeActivity.class);
		intent.putExtra("activity_type", resource);
		intent.putExtra("name", content.substring(1, content.length()));
		context.startActivity(intent);
		
	}

	@Override
	public void updateDrawState(TextPaint ds) {
		ds.setUnderlineText(false);
		ds.setColor(Color.BLUE);
	}
	
}

class StatusTopicSpan extends ClickableSpan
{
	Context context;
    String content;
    String resource;
	public StatusTopicSpan(Context context,String content,String resource) {
		super();
		this.content=content;
		this.context = context;
		this.resource=resource;
	}

	@Override
	public void onClick(View widget) {
		Intent intent =new Intent(context,TopicActivity.class);
		intent.putExtra("resource",resource );
		intent.putExtra("title", content.substring(1,content.length()-1));
		context.startActivity(intent);
	}

	@Override
	public void updateDrawState(TextPaint ds) {
		ds.setUnderlineText(false);
		ds.setColor(Color.BLUE);
	}
	
}

class StatusOtherSpan extends ClickableSpan
{
	Context context;
    String content;
    String id;
    String resource;

	public StatusOtherSpan(Context context, String content, String id,
			String resource) {
		super();
		this.context = context;
		this.content = content;
		this.id = id;
		this.resource = resource;
	}

	@Override
	public void onClick(View widget) {
		if(resource.equals("Renn"))
		{
			return;
		}
		Intent intent =new Intent(context,WeiBoDetailActivity.class);
		intent.putExtra("weiboID",id);
		intent.putExtra("resource",resource);
		context.startActivity(intent);
		
	}

	@Override
	public void updateDrawState(TextPaint ds) {
		ds.setColor(Color.BLACK);
		ds.setUnderlineText(false);
	}
	
}


class StatusHttpSpan extends ClickableSpan
{
	Context context;
    String content;
	public StatusHttpSpan(Context context,String content) {
		super();
		this.content=content;
		this.context = context;
	}

	@Override
	public void onClick(View widget) {
		Intent intent=new Intent(context,WebViewActivity.class);
		intent.putExtra("url", content);
		context.startActivity(intent);
	}

	@Override
	public void updateDrawState(TextPaint ds) {
		ds.setColor(Color.BLUE);
		ds.setUnderlineText(true);
	}
	
	
}

class Length
{
	public Length(int sIndex, int eIndex) {
		super();
		this.sIndex = sIndex;
		this.eIndex = eIndex;
	}
	
	
	public int sIndex;
	public int eIndex;
}